package homework;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
/**
 * 1. thread：线程
 *    2. interrupt：中断
 *    3. sleep：睡眠
 *    4. start：开始
 *    5. block：阻塞
 *    6. runnable：可运行
 *    7. current：当前的
 *    8. handler：处理器
 *    9. priority：优先级
 *    10. alive：活着
 *    11. daemon：守护线程
 */

/*** 聊天室客户端
 * */
public class Cilent {
    private Socket socket;

    public Cilent() {
        try {
            System.out.println("正在连接服务器...");
            socket = new Socket("localhost", 8090);
            System.out.println("已与服务器取得连接");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);
            Scanner scan = new Scanner(System.in);
            String line;
            while (true) {
                line = scan.nextLine();
                if("exit".equals(line)){
                    break;
                }
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Cilent cilent = new Cilent();
        cilent.start();

    }
}
